﻿/// <reference path="../JScript/jquery-1.6.min.js" />
/// <reference path="../JScript/jquery.easyui.min.js" />
/// <reference path="../JScript/AutoResize.js" />

var url = '../Handler/AjaxHandle.ashx';
$(function() {
    if (jQuery.url.param("ID") != undefined) {
        LoadBasicInfo();
    }
    $('#btnSubmit,#btnCancel').linkbutton();
    $('#btnCancel').bind("click", function() {
        $('#basicInfo').form("clear");
    });

    $("#btnSubmit").bind("click", AddBasicInfo);
    $("#CardID").bind("blur", function() {
        //获取输入身份证号码
        var uUserCard = $("#CardID").val();
        //获取出生日期
        var birthday = uUserCard.substring(6, 10) + "-" + uUserCard.substring(10, 12) + "-" + uUserCard.substring(12, 14);
        $("#Birthday").datebox("setValue", birthday);

        //获取性别
        if (parseInt(uUserCard.substr(16, 1)) % 2 == 1) {
            //男 
        } else {
            //女
        }

        //获取年龄 
        myDate = new Date();
        var month = myDate.getMonth() + 1;
        var day = myDate.getDate();
        var age = myDate.getFullYear() - uUserCard.substring(6, 10) - 1;
        if (uUserCard.substring(10, 12) < month || uUserCard.substring(10, 12) == month && uUserCard.substring(12, 14) <= day) {
            age++;
        }
        $("#Age").val(age);
    });

    $("#FirstWorkTime").datebox({
        required: true,
        onSelect: function(date) {
            //获取工龄
            myDate = new Date();
            var firstWorkTime = $("#FirstWorkTime").datebox('getValue');
            var dt2 = new Date(Date.parse(firstWorkTime));
            var month = myDate.getMonth() + 1;
            var day = myDate.getDate();
            var workingYear = myDate.getFullYear() - dt2.getFullYear();
            if (dt2.getMonth() < month || dt2.getMonth() == month && dt2.getDay() <= day) {
                workingYear++;
            }
            $("#WorkingYears").val(workingYear);
        }
    });

    $("#CompanyWorkTime").datebox({
        required: true,
        onSelect: function(date) {
            //获取司龄
            myDate = new Date();
            var firstWorkTime = $("#CompanyWorkTime").datebox('getValue');
            var dt2 = new Date(Date.parse(firstWorkTime));
            var month = myDate.getMonth() + 1;
            var day = myDate.getDate();
            var companyWork = myDate.getFullYear() - dt2.getFullYear();
            if (dt2.getMonth() < month || dt2.getMonth() == month && dt2.getDay() <= day) {
                companyWork++;
            }
            $("#CompanyWorkingYears").val(companyWork);
        }
    });
    
});
var myDate;
var p = { AssemblyName: "OA.Bll.Personnel", ClassName: "OA.Bll.Personnel.EmployeePresenter", Method: "Add", Options: {} };
function AddBasicInfo() {
    if (jQuery.url.param("type") == "edit") {
        p.Method = "Update";
        p.Options["Id"] = employeeId;
    }
    if ($("#basicInfo").form('validate')) {
        var array = $("#basicInfo").serializeArray();
        for (var i = 0; i < array.length; i++) {
            p.Options[array[i].name] = array[i].value;
        }
        p.Options["DeptId"] = $('#DeptName').combobox('getValue');
        p.Options["EmployeeImagePath"] = $("#EmployeeImagePath").attr("src");
        $.ajax({
            type: "post",
            url: "../Handler/AjaxHandle.ashx",
            data: $.toJSON(p),
            dataType: "json",
            success: function(data) {
                $.messager.alert('提示', data.msg, 'info');
            }
        });
    }
}

var employeeId;
function LoadBasicInfo() {
    p.Method = "GetSearchAll";
    employeeId = jQuery.url.param("ID");
    p.Options["ID"] = employeeId;
    $.ajax({
        type: "post",
        url: "../Handler/AjaxHandle.ashx",
        data: $.toJSON(p),
        dataType: "json",
        success: function(data) {
            $('#basicInfo').form('load', data.EmployeeInfo[0]);
            $("#EmployeeImagePath").attr("src", data.EmployeeInfo[0].EmployeeImagePath);
        }
    });
}

function Msgshow(msg) {
    $.messager.alert('提示', msg, 'info');
}
function Msgslide(msg) {
    $.messager.show({
        title: '提示',
        msg: msg,
        timeout: 3000,
        showType: 'slide'
    });
}
function Msgfade(msg) {
    $.messager.show({
        title: '提示',
        msg: msg,
        timeout: 3000,
        showType: 'fade'
    });
}
      
          